package com.topisv.tms.service.work.inscope;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.ProcessLog;
import com.topisv.tms.entity.work.inscope.InScope;
import com.topisv.tms.entity.work.outScope.OutScope;
import com.topisv.tms.entity.work.outScope.ScopeCustomerConfig;
import com.topisv.tms.repository.ProcessLogDao;
import com.topisv.tms.repository.UserDao;
import com.topisv.tms.repository.work.inscope.InScopeDao;
import com.topisv.tms.repository.work.outScope.ScopeCustomerConfigDao;

/**
 * 优势区配置_service
 * @author ch
 */
@Component
@Transactional(readOnly = true)
public class InScopeService {

	@Autowired
	private InScopeDao inScopeDao;

	@Autowired
	private ScopeCustomerConfigDao configDao;
	@Autowired
	private ProcessLogDao procssLogDao;
	@Autowired
	private UserDao userDao ;
	
	public List<InScope> loadByCompanyCode(String companyCode,String warehouseCode) {
		return this.inScopeDao.loadByCompanyCode(companyCode,warehouseCode);
	}
	public List<InScope> loadInScope(Map<String, String> param) {
		Specification<InScope> spec = getWhereClause(param);
		List<InScope> inScopeList= this.inScopeDao.findAll(spec);
		for(InScope inscope:inScopeList){
			inscope.setUser(userDao.findByJobuuid(inscope.getCreaterUuid()));
		}
		return inScopeList;
	}
	
	public InScope loadInScopeOne(Long id) {
		InScope inScope= this.inScopeDao.findOne(id);
		return inScope;
	}
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<InScope> getWhereClause(final Map<String, String> param) {
		
		return new Specification<InScope>(){
			
		    @Override
		    public Predicate toPredicate(Root<InScope> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      String companyCode=(String) param.get("companyCode");
		      if(StringUtils.isNotEmpty(companyCode)){
					pre.getExpressions().add(cb.equal(r.get("companyCode"),companyCode));
			  }

		      String warehouseCode=(String) param.get("warehouseCode");
		      if(StringUtils.isNotEmpty(warehouseCode)){
		    	  pre.getExpressions().add(cb.equal(r.get("warehouseCode"),warehouseCode));
		      }
		      return pre;
		    }
		  };
       }
	/**
	 * 添加超区
	 * @param companyCode
	 * @param zoneName
	 * @param zoneFullName
	 * @param jobuuid
	 * @return
	 */
	@Transactional(readOnly = false)
	public String addInScope(String companyCode, String zoneName,String zoneFullName,String zoneType,String warehouseCode,String jobuuid) {
		try {
			
			InScope inScope=this.inScopeDao.loadByZoneFullName(companyCode,zoneFullName,warehouseCode);
			if(inScope!=null){
				return "error_exist"; //已经存在此超区
			}
			
			inScope=new InScope();
			inScope.setCompanyCode(companyCode);
			inScope.setCreaterUuid(jobuuid);
			inScope.setCreateTime(new Date());
			inScope.setZoneFullName(zoneFullName);
			inScope.setZoneName(zoneName);
			inScope.setZoneType(zoneType);
			inScope.setWarehouseCode(warehouseCode);
			this.inScopeDao.save(inScope);
			
			ProcessLog pLog = new ProcessLog("IN_SCOPE", "仓库:" + inScope.getWarehouseCode() + "快递:"+companyCode+"添加了优势区 ("+inScope.getZoneFullName() + ")");
			this.procssLogDao.save(pLog);
			return "success";
			
		} catch (Exception e) {
			
			return "error";
		}
	}

	/**
	 * 删除
	 * @param InScopeId
	 * @return
	 */
	@Transactional(readOnly = false)
	public void deleteInScope(Long inScopeId) {
		InScope inScope = this.inScopeDao.findOne(inScopeId);
		
		this.inScopeDao.delete(inScopeId);
		
		ProcessLog pLog = new ProcessLog("IN_SCOPE", "仓库:" + inScope.getWarehouseCode() + "快递:"+inScope.getCompanyCode()+"删除了优势区 ("+inScope.getZoneFullName() + ")");
		this.procssLogDao.save(pLog);
	}
	public void saveInScopeConfigCustomers(String inScopeId,String customerCodes) {
		InScope inScope = this.inScopeDao.findOne(Long.valueOf(inScopeId));
		String[] customerCodeArray = customerCodes.split(",");
		//删除优势区适用商户配置
		this.configDao.deleteByInScopeId(Long.valueOf(inScopeId));
		List<ScopeCustomerConfig> configList = new ArrayList<ScopeCustomerConfig>();
		
		//添加配置适用商户
		for(String customerCode:customerCodeArray){
			if(StringUtils.isEmpty(inScopeId)){
				continue;
			}
			ScopeCustomerConfig config = new ScopeCustomerConfig();
			config.setOutScopeId(null);
			config.setInScopeId(Long.valueOf(inScopeId));
			config.setCustomerCode(customerCode);
			config.setCreateTime(new Date());
			configList.add(config);
		}
		//保存适用商户配置
		this.configDao.save(configList);
		
		ProcessLog pLog = new ProcessLog("IN_SCOPE_CONFIG","快递:"+inScope.getCompanyCode()+" 修改了优势区不适用商户 ("+customerCodes+")");
		procssLogDao.save(pLog);
	}

}